package com.ysd.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysd.common.exception.RRException;
import com.ysd.common.utils.R;
import com.ysd.modules.sys.dao.TopicSysDao;
import com.ysd.modules.sys.entity.PaperInfo;
import com.ysd.modules.sys.dao.PaperInfosDao;
import com.ysd.modules.sys.entity.TestPaper;
import com.ysd.modules.sys.entity.Topic;
import com.ysd.modules.sys.service.PaperInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dapeng
 * @since 2021-07-19
 */
@Service
public class PaperInfoServiceImpl extends ServiceImpl<PaperInfosDao, PaperInfo> implements PaperInfoService {
    @Resource
    private TopicSysDao topicSysDao;

    /**
     * 自动组卷
     *
     * @return
     */
    @Override
    public boolean autoPaper(List<Map<String, Object>> objs) {
        for (int i = 0; i < objs.size(); i++) {
            /**
             * 根据章节id查询章节ID下边的题目
             */
            Map<String, Object> map = objs.get(i);
            QueryWrapper<Topic> qw = new QueryWrapper<>();
            qw.eq("chapter_id", map.get("id"));
            qw.eq("status", 1);
            qw.orderByAsc("newid()");
            List<Topic> topicList = topicSysDao.selectList(qw);
            /**
             * 获取指定数量 添加到试卷中
             */
            boolean check = true;
            while (check) {
                int count = 0;
                if (map.get("num") == null || map.get("num") == "") {
                    break;
                }
                for (int j = 0; j < Integer.parseInt(map.get("num") + ""); j++) {
                    Topic topic = topicList.get(j);
                    PaperInfo paperInfo = new PaperInfo();
                    paperInfo.setPaperId(Integer.parseInt(map.get("tid") + ""));
                    paperInfo.setTopicId(topic.getId());
                    /**
                     * 判断试卷中是否有该试题，如果没有该试题，则添加，否则跳过
                     */
                    QueryWrapper<PaperInfo> pw = new QueryWrapper<>();
                    pw.eq("topic_id", paperInfo.getTopicId());
                    pw.eq("paper_id", paperInfo.getPaperId());
                    PaperInfo paperInfo1 = baseMapper.selectOne(pw);
                    /**
                     * 为了防止 题目数量和添加数量过小导致死循环进行统计，如果count == map.get("num") 则退出
                     */
                    count++;
                    if (paperInfo1 == null) {
                        int len = baseMapper.insert(paperInfo);
                        if (len == 0) {
                            return false;
                        }
                    } else {
                        if (count == Integer.parseInt(map.get("num") + "")) {
                            check = false;
                        }
                        continue;
                    }
                }
            }
        }
        return true;
    }

    /**
     * 根据试卷id查询所有的试卷题目
     *
     * @param tid
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage<PaperInfo> getTopicListByTid(Integer tid, Integer page, Integer limit) {
        QueryWrapper<PaperInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("paper_id", tid);
        Page<PaperInfo> currPage = new Page<>();
        currPage.setCurrent(page);
        currPage.setSize(limit);
        IPage<PaperInfo> iPage = baseMapper.selectPage(currPage, wrapper);
        for (int i = 0; i < iPage.getRecords().size(); i++) {
            PaperInfo paperInfo = iPage.getRecords().get(i);
            Topic topic = topicSysDao.selectById(paperInfo.getTopicId());
            paperInfo.setTopicName(topic.getTitle());
        }
        return iPage;
    }

    /**
     * 根据id 删除paperinfo中的题目
     *
     * @param id
     * @return
     */
    @Override
    public boolean delPaperInfo(Integer id, Integer tid) {
        QueryWrapper<PaperInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("paper_id", tid);
        queryWrapper.eq("topic_id", id);
        int len = baseMapper.delete(queryWrapper);
        if (len > 0) {
            return true;
        }
        return false;
    }

    /**
     * 想指定试卷增加试题
     *
     * @param pid
     * @param tid
     * @return
     */
    @Override
    public boolean addPaperInfo(Integer pid, Integer tid) {
        if (pid == null || pid == 0) {
            throw new RRException("试卷ID不能为空");
        }
        if (tid == null || tid == 0) {
            throw new RRException("题目ID不能为空");
        }
        QueryWrapper<PaperInfo> qw = new QueryWrapper<>();
        qw.eq("topic_id", tid);
        qw.eq("paper_id", pid);
        PaperInfo paperInfo = baseMapper.selectOne(qw);
        if (paperInfo != null){
            return false;
        }
        paperInfo = new PaperInfo();
        paperInfo.setPaperId(pid);
        paperInfo.setTopicId(tid);
        int len = baseMapper.insert(paperInfo);
        if (len > 0) {
            return true;
        }
        return false;
    }

}
